﻿/*
 * ============ [ 废土 ] ============
 *
 * [ 维护者 ]  argoran@gmail.com
 * [  状态  ]  原型
 *
 * ============ [ 注释 ] ============
 *
 * 处理10号消息的类
 *
 * ==================================
 */

using System;

using LastLand.Data.Global;
using LastLand.Data.Connection;

using Up2Link.SegString;
using Up2Link.Server;

namespace LastLand.Core.MsgProcessor
{
    public class CMsgNewAccount : IMessageProcessor
    {
        public void ProcessMessage(CMessagePack<CMessageSS> _msgPack)
        {
            CMessageSS msg = _msgPack.message;
            Int32 index = _msgPack.index;

            String[] data = msg.MsgContent.Split(CConst.DELIMITER_USERACCOUNT);
            String username = data[0];
            String password = data[1];

            if (!CData.connections[index].IsLoggedIn() && !CData.userAccounts.ContainsKey(username))
            {
                CData.userAccounts.Add(username, new CUserAccount(username, password, -1, null));
                CMessageSS okNewAccountMsg = new CMessageSS(CConst.SERVER_MESSAGE_COMPLETE_NEW_ACCOUNT, 0, CMessageSS.ENCODING_UTF32, "");
                CMessagePack<CMessageSS> okNewAccountMsgPack = new CMessagePack<CMessageSS>(okNewAccountMsg, index);
                CData.server.Send(okNewAccountMsgPack);
            }
            else if (CData.userAccounts.ContainsKey(username))
            {
                CMessageSS invalidAccountNameMsg = new CMessageSS(CConst.SERVER_MESSAGE_INVALID_USERNAME_OR_PASSWORD, 0, CMessageSS.ENCODING_UTF32, "");
                CMessagePack<CMessageSS> invalidAccountNameMsgPack = new CMessagePack<CMessageSS>(invalidAccountNameMsg, index);
                CData.server.Send(invalidAccountNameMsgPack);
            }
            else
            {
                CMessageSS loggedInMsg = new CMessageSS(CConst.SERVER_MESSAGE_CLIENT_LOGGED_IN, 0, CMessageSS.ENCODING_UTF32, "");
                CMessagePack<CMessageSS> loggedInMsgPack = new CMessagePack<CMessageSS>(loggedInMsg, index);
                CData.server.Send(loggedInMsgPack);
            }
        }
    }
}
